import {order as orderApi,member as memberApi} from '@/http'
export default {
    namespaced: true,
    state: {
        shopsetting : {
            pay : {},
            shop : {},
            trade : {}
        },

        cardata : [],
        carloading : false,

        paymentdata : [],
        buynow : 0,

        address : [],
        defaultaddress : 0,
        addressloading : false,

    },
    mutations: {
        shopsetting(state,data){
            state.shopsetting = data;
        },
        cardata(state,data){
            state.cardata = data;
        },
        carloading(state,data){
            state.carloading = data;
        },
        paymentdata(state,data){
            state.paymentdata = data;
        },
        buynow(state,data){
            state.buynow = data;
        },
        address(state,data){
            state.address = data;
        },
        addressloading(state,data){
            state.addressloading = data;
        },
        defaultaddress(state,data){
            state.defaultaddress = data;
        },
    },
    getters : {
        defaultaddress : state => {
            let data = state.address.filter(i => i.id == state.defaultaddress);
            if(data.length == 1){
                return data[0]
            }else{
                return {}
            }
        }
    },
    actions : {
        initshopsetting({commit,state},update){
            if(state.shopsetting.pay.wepay_open && !update) return false;
            orderApi.get('shop/sets').then(res => {
                if(!res.data.code){
                    commit('shopsetting',res.data.data)
                }
            })
        },
        initshopcar({commit,state},update){
            if(state.cardata.length && !update) return false;
            commit('carloading',true)
            orderApi.get('shop/cart').then(res => {
                commit('carloading',false)
                if(!res.data.code){
                    commit('cardata',res.data.data.map(i => {
                        return Object.assign(i,{
                            checked : false
                        })
                    }))
                }
            })
        },
        initaddress({commit,state},update){
            if(state.address.length && !update) return false;
            commit('addressloading',true)
            memberApi.get('vipaddress').then(res => {
                commit('addressloading',false)
                if(!res.data.code){
                    if(!state.defaultaddress){
                        let defaultadd = res.data.data.data.filter(f => f.isdefault);
                        if(defaultadd.length){
                            state.defaultaddress = defaultadd[0].id
                        }
                    }
                    commit('address',res.data.data.data)
                }
            })
        }
    }
};
